<template>
  <el-dialog v-if="visible" :title="info.title" :visible.sync="visible" append-to-body center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="120px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="pro">
            <ProSelectObject v-model="form.pro" :id="form.pro_id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="PO单号">
            <span v-if="form.pro.no">{{ "PO"+form.pro.no }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="开工日期">
            <span v-if="form.pro.no">{{ baogao.begin_time }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="计划完工日期">
            <span v-if="form.pro.no">{{ baogao.plan_complete_time }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资料申请状态" prop="state">
            <el-select v-model="form.state" size="small" style="width: 100%">
              <el-option
                v-for="item in states"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="进度描述" prop="type">
            <el-select v-model="form.type" size="small" style="width: 100%">
              <el-option
                v-for="(item, index) in types"
                :key="index"
                :label="item.value"
                :value="index"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件上传" prop="files">
            <Upload v-model="form.files" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable style="width: 100%" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="抄送" prop="ccs">
            <VerifyCC v-model="form.ccs" :ccs="ccs" />
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleBeforeTo()">提 交</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import ProSelectObject from '@/myComponents/ProSelectObject'
// import VerifyCC from '@/myComponents/VerifyCC'
import Upload from '@/myComponents/Upload'
import { baogaoInfo } from '@/api/project/engineering'
import { payType } from '@/api/project'
import svf from '../../util/svf'

export default {
  components: { ProSelectObject, Upload },
  extends: svf,
  data() {
    const vnull = (rule, value, callback) => {
      if (JSON.stringify(value) === '{}') {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    }
    return {
      types: {},
      baogao: {},
      approve_key: 'PROCESS_FUND',
      states: [{ key: '未提交', value: '未提交' }, { key: '已提交', value: '已提交', validator: vnull }],
      rules: {
        pro: [{ required: true, trigger: 'blur', message: '项目名称不可为空' }],
        des: [{ required: true, trigger: 'blur', message: '进度描述不可为空' }],
        state: [{ required: true, trigger: 'blur', message: '资料申请状态不可为空' }],
        files: [{ required: true, trigger: 'blur', message: '附件不可为空' }]
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        if(this.info.type && this.info.type === 'again'){
          this.initApply(this.info.edit)
        }else{
          this.form = this.info.edit
        }
        this.$set(this.form, 'pro', {})
        this.payType()
        // this.queryCcs()
      }
    },
    'form.pro': {
      handler(n, o) {
        if (this.form.pro.key) {
          this.baogaoInfo(this.form.pro.key)
        }
      },
      deep: true
    }
  },
  mounted() {
  },
  methods: {
    payType() {
      payType().then(response => {
        this.types = response.info
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    baogaoInfo(e) {
      baogaoInfo({ pro_id: e }).then(response => {
        this.baogao = response.info
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleBeforeTo() {
      this.validate().then(() => {
        const e = {}
        e.files = this.form.files
        e.approve_key = this.approve_key
        e.des = '项目名称：' + this.form.pro.value + '    备注：' + this.form.remark
        e.ccs = this.form.ccs
        e.forms = JSON.stringify(this.createForms([
          ['PO单号', 'PO' + this.form.pro.no, '', ''],
          ['项目名称', this.form.pro.value, this.form.pro.key, 'pro_id'],
          ['开工日期', this.baogao.begin_time, '', ''],
          ['计划完工日期', this.baogao.plan_complete_time, '', ''],
          ['资料申请状态', this.form.state, this.form.state, 'state'],
          ['进度描述', this.types[this.form.type]['value'], this.types[this.form.type]['key'], 'type'],
          ['备注', this.form.remark, this.form.remark, 'remark']
        ]))
        this.handleCreate(e)
      })
    }
  }
}
</script>

<style>
</style>
